package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页查询

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO>page =setmealMapper.pageQuery(setmealPageQueryDTO);
        long total =page.getTotal();
        List<SetmealVO> records =page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 新增套餐和对应的菜品
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void xinzengWithFlavor(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表中插入一条数据
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();
        log.info("套餐id{}",setmealId);
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish->{
            log.info("套餐setmealDish{}", setmealDish);
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);

        }

    /**
     * 删除套餐
     * @param ids
     */
    //开始事务功能
    @Transactional
    @Override
    public void deleteBatach(List<Long> ids) {
        //判断套餐是否在售卖，售卖中的套餐不能删
        for (Long id : ids) {
            SetmealVO setmeal =setmealMapper.getById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                //如果套餐是在售卖的删除则抛错（起售中的套餐不可删除）
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.DeleteSetmeal(ids);
            setmealDishMapper.getSetmealIdsByDishIds(ids);
        }
    }

    /**
     * 套餐的停售和起售
     * @param status
     * @param id
     */

    @Override
    public void sOs(Integer status, Long id) {
    Setmeal setmeal =Setmeal.
            builder()
            .status(status)
            .id(id)
            .build();
    setmealMapper.update(setmeal);
    }
    /**
     * 根据前端返回的id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO setmealById(Long id) {
        SetmealVO setmealVO =setmealMapper.getById(id);
        List<SetmealDish> bySetmealId =setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(bySetmealId);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateWithFlavor(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //重新插入菜品数据
        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();
         ArrayList<Long> arrayList = new ArrayList<>();
         arrayList.add(setmeal.getId());

         setmealDishMapper.DeleteSetmeal(arrayList);
        //向菜品插入n条数据
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
            setmealDishMapper.insertBatch(setmealDishes);
        //修改套餐表的基本信息
        setmealMapper.update(setmeal);
        }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    }






